package cn.example.vip.mall.search.controller;

import cn.example.vip.mall.search.feign.SkuSearchFeign;
import cn.example.vip.mall.util.RespResult;
import cn.example.vip.mall.util.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 页面响应
 */
@CrossOrigin
@Controller
@RequestMapping("/web/search")
public class SearchController {
	@Value("${extern.server.address}")
	private String externServerAddress;
	
	@Autowired
	private SkuSearchFeign skuSearchFeign;
	
	/**
	 * 跳转到 Search.html 页面
	 */
	@GetMapping
	public String search(@RequestParam(required = false) Map<String, Object> searchMap,
						 Model model,
						 @Autowired HttpServletResponse response){
		RespResult<Map<String, Object>> resultResp = skuSearchFeign.search(searchMap);
		System.out.println("访问:" + resultResp.getData());
		// 组装用户访问的url(当前请求地址组装),(排除page参数回显)
		model.addAttribute("url", UrlUtils.map2url("/web/search", searchMap, "page", "sfield", "sm"));
		// 装入返回结果
		model.addAttribute("result", resultResp.getData());
		// 装入回显的参数
		model.addAttribute("searchMap", searchMap);
		// 装入服务address
		model.addAttribute("externServerAddress", externServerAddress);
		return "search";
	}


}
